﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FineUI;
using SysManage.BLL;
using ZHDJ.Core.Util;
using ZHDJ.Data.Models.Center;
using ZHDJ.Data.Models.Common;
using ZHDJ.WebPage;

namespace SysManage.Module.ScoreManage
{
    public partial class PartyObjectScoreRuleEdit : PageBase
    {
        #region 属性
      
        #endregion
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get { return "PartyObjectScoreRuleAdd"; }
        }

        #endregion

        #region 页面属性

        public string EditorContent { get; set; }

        #endregion

        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                BindData();
                LoadData();
            }
        }

        private void BindData()
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
           
        }

        private void LoadData()
        {
            string id = GetQueryIntValue("id");
            if (id != "")
            {
                var current = DB.PartyObjectScoreRules.Find(id);
                if (current == null)
                {
                    // 参数错误，首先弹出Alert对话框然后关闭弹出窗口
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }
                txtName.Text = current.Name;
                txtCategory.Text = current.Category;
                txtScore.Text = current.Score.ToString("F2");
                txtDayMax.Text = current.DayMax.ToString("F2");
                txtMonthMax.Text = current.MonthMax.ToString("F2");
                txtYearMax.Text= current.YearMax.ToString("F2");
                ddlStatus.SelectedValue = current.Status.ToString();
                ddlObjectType.SelectedValue = current.ObjectType;
                this.ddlObjectType.Readonly = true;
                this.txtCategory.Readonly = true;
            }
            else
            {
                txtDayMax.Text = "0";
                txtMonthMax.Text = "0";
                txtYearMax.Text = "0";
            }

        }
        
        #endregion

        #region Events

        private void SaveData()
        {
            if (Convert.ToDecimal(txtDayMax.Text) > Convert.ToDecimal(txtMonthMax.Text) && Convert.ToDecimal(txtMonthMax.Text)>0)
            {
                Alert.Show("每月上限不能小于每日上限，请重新输入！", String.Empty, ActiveWindow.GetHideReference());
                return;
            }
            string id = GetQueryIntValue("id");
            string imageUrl = string.Empty;
            PartyObjectScoreRule editModel;
            if (id != "")
            {
                //更新
                editModel = DB.PartyObjectScoreRules.Find(id);
                if (editModel == null)
                {
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }
                editModel.ModifyTime = DateTime.Now;
                editModel.ModifyBy = CurrentUser.LoginID;
            }
            else
            {
                var current = DB.PartyObjectScoreRules.Where(a=>a.Category== this.txtCategory.Text.Trim());
                if (current.Any())
                {
                    Alert.Show("积分标识已存在，请重新输入！", String.Empty);
                    return;
                }
                editModel = new PartyObjectScoreRule();
                editModel.ID = StringHelper.NewID;
                editModel.CreateTime = DateTime.Now;
                editModel.ModifyTime = DateTime.Now;
                editModel.CreateBy = CurrentUser.LoginID;
                editModel.ModifyBy = CurrentUser.LoginID;
                DB.PartyObjectScoreRules.Add(editModel);
            }

            editModel.Name = txtName.Text;
            editModel.Category = txtCategory.Text;
            editModel.Score = Convert.ToDecimal(txtScore.Text);
            editModel.DayMax = Convert.ToDecimal(txtDayMax.Text);
            editModel.Status = Convert.ToInt32(ddlStatus.SelectedValue);
            editModel.HourMax = 0;
            editModel.MonthMax = Convert.ToDecimal(txtMonthMax.Text); 
            editModel.WeekMax = 0;
            editModel.YearMax = Convert.ToDecimal(txtYearMax.Text); 
            editModel.ObjectType = ddlObjectType.SelectedValue;
            if (DB.SaveChanges() > 0)
            {
                
                PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
            }

        }

        /// <summary>
        /// 保存并关闭按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            SaveData();


        }


        #endregion

    }
}